﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CARMS
{
    public partial class DownLoadFile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var strFolderExist = (Request.QueryString["folderExist"] == "1");

            string path = string.Empty;
            if (!strFolderExist)
            {
                path = Server.MapPath("~/Files/" + Server.UrlDecode(Request.QueryString["path"]));
            }
            else
            {
                path=Server.UrlDecode(Request.QueryString["path"]);
            }
            if (File.Exists(path))
            {
                string name = Request.QueryString["name"];
                if (string.IsNullOrEmpty(name))
                {
                    name = new FileInfo(path).Name;
                }
                Response.AddHeader("Content-Disposition",
                                   "attachment;filename=\"" + Server.UrlDecode(name) + "\"");
                byte[] buffer = File.ReadAllBytes(path);
                Response.AddHeader("Content-Length", buffer.Length.ToString());
                Response.ContentType = "application/octet-stream;charset=utf-8";
                Response.BinaryWrite(buffer);
            }
        }
    }
}
